/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVK;

import DAO.LoaiSanPhamDao;
import DAO.SanPhamDao;
import POJO.LoaiSanPham;
import POJO.SanPham;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Kim
 */
public class frQuanLySanPham extends javax.swing.JInternalFrame {

	private DefaultComboBoxModel cbModel;
	private DefaultTableModel tbmodel1;
	private DefaultTableModel tbmodel2;
	private int TSD;// tong so dong trong bang hoa don
	private int THT; //trang hiện tại
	private int SD;// hien thi bao nhieu dong tron trang

	/**
	 * Creates new form frQuanLySanPham
	 */
	public frQuanLySanPham() {
		initComponents();
		this.cbModel = new DefaultComboBoxModel();
		this.jcbLoaiSP.setModel(cbModel);
		LoadLoaiSanPham();
		LoadLaiDuLieu();
	}

	private void LoadLaiDuLieu() {
		TSD = soDongDuLieu();
		SD = 20;
		THT = 0;
		load(SD, THT);
		setTxt();
	}

	private void LoadSanPham() {
		DefaultFormSanPham();

		ArrayList<SanPham> dsSanPham = SanPhamDao.layDanhSach();

		LoaiSanPhamDao dao = new LoaiSanPhamDao();
		int i = 1;
		for (SanPham sp : dsSanPham) {
			int maLSP = sp.getMaLSP();
			LoaiSanPham lsp = dao.layByMa(maLSP);
			Object[] items = new Object[]{
				i,
				sp.getMaSP(),
				sp.getTenSP(),
				sp.getSlTon(),
				sp.getSlTonToiThieu(),
				sp.getThoiGianBaoHanh(),
				sp.getMoTa(),
				sp.getGiaBanHienHanh(),
				lsp.getTenLSP()
			};
			Object[] items2 = new Object[]{
				i,
				sp.getMaSP(),
				sp.getTenSP(),
				sp.getSlTon(),
				sp.getSlTonToiThieu(),
				sp.getThoiGianBaoHanh(),
				sp.getMoTa(),
				sp.getGiaBanHienHanh(),
				sp.getMaLSP()
			};
			i++;
			//int maLSP = sp.getMaLSP();
			tbmodel1.addRow(items);
			tbmodel2.addRow(items2);
		}
		jtDanhSachSanPham.setModel(tbmodel1);
	}

	private int soDongDuLieu() {
		int tmp = 0;
		SanPhamDao dao = new SanPhamDao();
		try {
			tmp = dao.laySoDong();
		} catch (Exception e) {
		}
		return tmp;
	}

	public String KiemTraMa() {
		String str = "";
		if (jtfMaSP.getText().equals("")) {
			str += "- Xin chọn mã sản phẩm trên lưới ! \r\n";
		}
		return str;
	}

	public String KiemTraDuLieu() {
		String str = "";
		if (jtfTenSP.getText().equals("")) {
			str += "- Chưa nhập tên sản phẩm ! \r\n";
		}
		if (jtfSoLuongTonHienTai.getText().equals("")) {
			str += "- Chưa nhập số lượng tồn hiện tại ! \r\n";
		}
		if (jcbTGBH.getSelectedItem().equals("")) {
			str += "- Chưa chọn thời gian bảo hành ! \r\n";
		}
		if (jtfSoLuongTonToiThieu.getText().equals("")) {
			str += "- Chưa nhập số lượng tồn tối thiểu ! \r\n";
		}
		if (jtfGiaBan.getText().equals("")) {
			str += "- Chưa nhập giá bán ! \r\n";
		}
		if (jcbLoaiSP.getSelectedItem().equals("")) {
			str += "- Chưa chọn loại sản phẩm ! \r\n";
		}
		if (jtaMoTa.getText().equals("")) {
			str += "- Chưa nhập mô tả ! \r\n";
		}
		return str;
	}

	public void DefaultFormSanPham() {
		tbmodel1 = new DefaultTableModel();
		tbmodel2 = new DefaultTableModel();
		//tạo các cột
		this.tbmodel1.addColumn("STT ");
		this.tbmodel1.addColumn("Mã SP");
		this.tbmodel1.addColumn("Tên sản phẩm");
		this.tbmodel1.addColumn("SL tồn");
		this.tbmodel1.addColumn("Giá bán");
		this.tbmodel1.addColumn("Loại sản phẩm");

		this.tbmodel2.addColumn("STT ");
		this.tbmodel2.addColumn("Mã SP");
		this.tbmodel2.addColumn("Tên SP");
		this.tbmodel2.addColumn("SL tồn");
		this.tbmodel2.addColumn("Tồn tối thiểu");
		this.tbmodel2.addColumn("TG bảo hành ");
		this.tbmodel2.addColumn("Mô tả");
		this.tbmodel2.addColumn("Giá bán");
		this.tbmodel2.addColumn("Mã LSP");
	}

	public void load(int soDong, int trangHienTai) {
		jbtnThem.setEnabled(false);
		jbtnMoi.setEnabled(true);
		DefaultFormSanPham();

		TSD = soDongDuLieu();

		ArrayList<SanPham> dsSanPham = null;
		try {
			dsSanPham = SanPhamDao.layDanhSachPhanTrang(soDong, trangHienTai);

		} catch (Exception e) {
		}

		LoaiSanPhamDao dao = new LoaiSanPhamDao();
		int i = 1;
		for (SanPham sp : dsSanPham) {
			int maLSP = sp.getMaLSP();
			LoaiSanPham lsp = dao.layByMa(maLSP);
			Object[] items = new Object[]{
				i,
				sp.getMaSP(),
				sp.getTenSP(),
				sp.getSlTon(),
				sp.getGiaBanHienHanh(),
				lsp.getTenLSP()
			};
			Object[] items2 = new Object[]{
				i,
				sp.getMaSP(),
				sp.getTenSP(),
				sp.getSlTon(),
				sp.getSlTonToiThieu(),
				sp.getThoiGianBaoHanh(),
				sp.getMoTa(),
				sp.getGiaBanHienHanh(),
				sp.getMaLSP()
			};
			i++;
			//int maLSP = sp.getMaLSP();
			tbmodel1.addRow(items);
			tbmodel2.addRow(items2);
		}
		jtDanhSachSanPham.setModel(tbmodel1);

		int[] va = {5, 15, 30, 10, 20, 20};
		int[] pt = new int[6];
		for (int l = 0; l < 6; l++) {
			pt[l] = jtDanhSachSanPham.getWidth() * va[l] / 100;
			jtDanhSachSanPham.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
		if (jtDanhSachSanPham.getRowCount() > 0) {
			loadSanPhamLenTexbox(0);
		}
	}

	public void loadSanPhamLenTexbox(int dong) {

		Object maSP = tbmodel2.getValueAt(dong, 1);
		Object tenSP = tbmodel2.getValueAt(dong, 2);
		Object slTon = tbmodel2.getValueAt(dong, 3);
		Object slTonToiThieu = tbmodel2.getValueAt(dong, 4);
		Object tgbh = tbmodel2.getValueAt(dong, 5);
		Object moTa = tbmodel2.getValueAt(dong, 6);
		Object giaBan = tbmodel2.getValueAt(dong, 7);
		Object loaiSP = tbmodel2.getValueAt(dong, 8);

		jtfMaSP.setText(maSP.toString());
		jtfTenSP.setText(tenSP.toString());
		jtfSoLuongTonHienTai.setText(slTon.toString());
		jcbTGBH.setSelectedItem(tgbh.toString());
		jtfSoLuongTonToiThieu.setText(slTonToiThieu.toString());
		jtfGiaBan.setText(giaBan.toString());
		jcbLoaiSP.setSelectedItem(loaiSP.toString());
		jtaMoTa.setText(moTa.toString());

	}
//    private void LoadLoaiSanPhamTheoMa(int maLSP)
//    {        
//        ArrayList<LoaiSanPham> dsLoaiSanPham = LoaiSanPhamDao.layByMa(maLSP);
//        String[] columns = new String [] { "Tên loại sản phẩm"};
//        DefaultTableModel model2 = new DefaultTableModel(null, columns);
//        for(LoaiSanPham lsp: dsLoaiSanPham) {
//            Object [] items2 = new Object [] {                
//                lsp.getTenLSP()                                
//            };      
//            model2.addRow(items2);
//        }        
//        jtDanhSachSanPham.setModel(model2);
//    }

	private void LoadLoaiSanPham() {

		LoaiSanPhamDao dao = new LoaiSanPhamDao();

		ArrayList<LoaiSanPham> ds = dao.layDanhSach();
		for (int i = 0; i < ds.size(); i++) {
			this.cbModel.addElement(ds.get(i));
		}

	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              jPanel3 = new javax.swing.JPanel();
              jPanel4 = new javax.swing.JPanel();
              jLabel2 = new javax.swing.JLabel();
              jLabel3 = new javax.swing.JLabel();
              jLabel4 = new javax.swing.JLabel();
              jLabel5 = new javax.swing.JLabel();
              jLabel6 = new javax.swing.JLabel();
              jLabel8 = new javax.swing.JLabel();
              jtfMaSP = new javax.swing.JTextField();
              jtfSoLuongTonHienTai = new javax.swing.JTextField();
              jtfSoLuongTonToiThieu = new javax.swing.JTextField();
              jtfGiaBan = new javax.swing.JTextField();
              jScrollPane1 = new javax.swing.JScrollPane();
              jtaMoTa = new javax.swing.JTextArea();
              jLabel7 = new javax.swing.JLabel();
              jtfTenSP = new javax.swing.JTextField();
              jLabel14 = new javax.swing.JLabel();
              jcbTGBH = new javax.swing.JComboBox();
              jcbLoaiSP = new javax.swing.JComboBox();
              jLabel12 = new javax.swing.JLabel();
              jLabel13 = new javax.swing.JLabel();
              jPanel6 = new javax.swing.JPanel();
              jScrollPane2 = new javax.swing.JScrollPane();
              jtDanhSachSanPham = new javax.swing.JTable();
              jPanel5 = new javax.swing.JPanel();
              jbtnThem = new javax.swing.JButton();
              jbtnXoa = new javax.swing.JButton();
              jbtnSua = new javax.swing.JButton();
              jbtnHienThi = new javax.swing.JButton();
              jButton1 = new javax.swing.JButton();
              jButton2 = new javax.swing.JButton();
              jButton3 = new javax.swing.JButton();
              jButton4 = new javax.swing.JButton();
              jlPhanTrang = new javax.swing.JLabel();
              jbtnMoi = new javax.swing.JButton();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel1.setText("QUẢN LÝ SẢN PHẨM");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Sản phẩm cần thêm"));
              jPanel4.setToolTipText("");

              jLabel2.setText("Mã sản phẩm:");

              jLabel3.setText("Số lượng tồn:");

              jLabel4.setText("SL tồn tối thiểu:");

              jLabel5.setText("Giá bán:");

              jLabel6.setText("Mô tả:");

              jLabel8.setText("Loại sản phẩm:");

              jtfMaSP.setEnabled(false);

              jtfSoLuongTonHienTai.setText("0");
              jtfSoLuongTonHienTai.setEnabled(false);

              jtfSoLuongTonToiThieu.setText("10");

              jtfGiaBan.setText("0");

              jtaMoTa.setColumns(20);
              jtaMoTa.setRows(5);
              jtaMoTa.setDropMode(javax.swing.DropMode.INSERT);
              jScrollPane1.setViewportView(jtaMoTa);

              jLabel7.setText("Tên sản phẩm:");

              jLabel14.setText("Thời gian bảo hành:");

              jcbTGBH.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "12", "24", "36" }));

              jLabel12.setText("VNĐ");

              jLabel13.setText("Tháng");

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel4Layout.createSequentialGroup()
                                          .addGap(19, 19, 19)
                                          .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                 .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(jcbTGBH, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(jLabel13))
                                   .addGroup(jPanel4Layout.createSequentialGroup()
                                          .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                 .addGroup(jPanel4Layout.createSequentialGroup()
                                                        .addGap(49, 49, 49)
                                                        .addComponent(jLabel8))
                                                 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                                        .addContainerGap()
                                                        .addComponent(jLabel6)))
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                 .addComponent(jtfTenSP)
                                                 .addComponent(jtfMaSP)
                                                 .addComponent(jtfSoLuongTonHienTai)
                                                 .addComponent(jtfSoLuongTonToiThieu)
                                                 .addComponent(jScrollPane1)
                                                 .addGroup(jPanel4Layout.createSequentialGroup()
                                                        .addComponent(jtfGiaBan, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(jLabel12))
                                                 .addComponent(jcbLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel2)
                                   .addComponent(jtfMaSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtfTenSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel7))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel3)
                                   .addComponent(jtfSoLuongTonHienTai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel14)
                                   .addComponent(jcbTGBH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel13))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel4)
                                   .addComponent(jtfSoLuongTonToiThieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel5)
                                   .addComponent(jtfGiaBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel12))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel8)
                                   .addComponent(jcbLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel4Layout.createSequentialGroup()
                                          .addComponent(jLabel6)
                                          .addGap(0, 90, Short.MAX_VALUE))
                                   .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
                            .addContainerGap())
              );

              jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách sản phẩm"));

              jtDanhSachSanPham.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Mã SP", "Tên sản phẩm", "SL tồn", "Giá bán", "Loại sản phẩm"
                     }
              ));
              jtDanhSachSanPham.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            jtDanhSachSanPhamMouseClicked(evt);
                     }
                     public void mouseEntered(java.awt.event.MouseEvent evt) {
                            jtDanhSachSanPhamMouseEntered(evt);
                     }
              });
              jScrollPane2.setViewportView(jtDanhSachSanPham);

              javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
              jPanel6.setLayout(jPanel6Layout);
              jPanel6Layout.setHorizontalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
              );
              jPanel6Layout.setVerticalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
              );

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );

              jbtnThem.setText("Thêm");
              jbtnThem.setEnabled(false);
              jbtnThem.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnThemActionPerformed(evt);
                     }
              });

              jbtnXoa.setText("Xóa");
              jbtnXoa.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnXoaActionPerformed(evt);
                     }
              });

              jbtnSua.setText("Sửa");
              jbtnSua.setToolTipText("");
              jbtnSua.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnSuaActionPerformed(evt);
                     }
              });

              jbtnHienThi.setText("Hiển thị");
              jbtnHienThi.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnHienThiActionPerformed(evt);
                     }
              });

              jButton1.setText("|<");
              jButton1.setToolTipText("HIển thị 30 dòng đầu tiên");
              jButton1.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton1ActionPerformed(evt);
                     }
              });

              jButton2.setText(">");
              jButton2.setToolTipText("Hiển thị 30 dòng sau");
              jButton2.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton2ActionPerformed(evt);
                     }
              });

              jButton3.setText("<");
              jButton3.setToolTipText("Hiển thị 30 dòng trước");
              jButton3.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton3ActionPerformed(evt);
                     }
              });

              jButton4.setText(">|");
              jButton4.setToolTipText("Hiển thị 30 dòng cuối cùng");
              jButton4.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton4ActionPerformed(evt);
                     }
              });

              jlPhanTrang.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
              jlPhanTrang.setText("0");

              jbtnMoi.setText("Mới");
              jbtnMoi.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnMoiActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
              jPanel5.setLayout(jPanel5Layout);
              jPanel5Layout.setHorizontalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addComponent(jbtnMoi)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtnThem)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtnXoa)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtnSua)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtnHienThi)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 208, Short.MAX_VALUE)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jlPhanTrang)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton4)
                            .addContainerGap(260, Short.MAX_VALUE))
              );
              jPanel5Layout.setVerticalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jbtnThem)
                                   .addComponent(jbtnXoa)
                                   .addComponent(jbtnSua)
                                   .addComponent(jbtnHienThi)
                                   .addComponent(jButton1)
                                   .addComponent(jButton3)
                                   .addComponent(jButton2)
                                   .addComponent(jButton4)
                                   .addComponent(jlPhanTrang)
                                   .addComponent(jbtnMoi))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

    private void jbtnHienThiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHienThiActionPerformed
	    // TODO add your handling code here:
	    LoadLaiDuLieu();
    }//GEN-LAST:event_jbtnHienThiActionPerformed
	private boolean kiemTraLoiNhapSaiSo() {
		try {// kiem tra nhap so luong ton
			int tmps = Integer.parseInt(jtfSoLuongTonHienTai.getText().trim());
			tmps = Math.abs(tmps);
			jtfSoLuongTonHienTai.setText(Integer.toString(tmps));
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhấp số ", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			jtfSoLuongTonHienTai.requestFocus();;
			jtfSoLuongTonHienTai.selectAll();
			return true;
		}

		try {// kiem số lượng tồn tối thiểu
			int tmps = Integer.parseInt(jtfSoLuongTonToiThieu.getText().trim());
			tmps = Math.abs(tmps);
			jtfSoLuongTonToiThieu.setText(Integer.toString(tmps));
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhấp số ", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			jtfSoLuongTonToiThieu.requestFocus();;
			jtfSoLuongTonToiThieu.selectAll();
			return true;
		}
		try {// kiem tra nhap gia
			double tmps = Double.parseDouble(jtfGiaBan.getText().trim());
			tmps = Math.abs(tmps);
			jtfGiaBan.setText(Double.toString(tmps));
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhấp số ", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			jtfGiaBan.requestFocus();;
			jtfGiaBan.selectAll();
			return true;
		}
		return false;
	}
    private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
	    // TODO add your handling code here:
	    String str = "";
	    str = KiemTraDuLieu();
	    if (kiemTraLoiNhapSaiSo()) {
		    return;
	    }

	    if (str == "") {
		    String tenSP = this.jtfTenSP.getText();
		    int slTon = Integer.parseInt(this.jtfSoLuongTonHienTai.getText());
		    int slTonToiThieu = Integer.parseInt(this.jtfSoLuongTonToiThieu.getText());
		    int thoigianBaoHanh = 0;
		    switch (Integer.parseInt(jcbTGBH.getSelectedItem().toString())) {
			    case 0:
				    thoigianBaoHanh = 0;
				    break;
			    case 12:
				    thoigianBaoHanh = 12;
				    break;
			    case 24:
				    thoigianBaoHanh = 24;
				    break;
			    case 36:
				    thoigianBaoHanh = 36;
				    break;
		    }
		    String moTa = this.jtaMoTa.getText();
		    float giaBanHienHanh = Float.parseFloat(this.jtfGiaBan.getText());
		    LoaiSanPham lsp = (LoaiSanPham) this.jcbLoaiSP.getSelectedItem();

		    SanPham sp = new SanPham();

		    sp.setTenSP(tenSP);
		    sp.setSlTon(slTon);
		    sp.setSlTonToiThieu(slTonToiThieu);
		    sp.setThoiGianBaoHanh(thoigianBaoHanh);
		    sp.setMoTa(moTa);
		    sp.setGiaBanHienHanh(giaBanHienHanh);
		    sp.setMaLSP(lsp.getMaLSP());

		    boolean kq = SanPhamDao.them(sp);
		    if (kq == true) {
			    JOptionPane.showMessageDialog(this, "Thêm sản phẩm thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
			    LoadLaiDuLieu();
			    jbtnThem.setEnabled(false);
		    } else {
			    JOptionPane.showMessageDialog(this, "Thêm sản phẩm thất bại", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);
		    }
	    } else {
		    JOptionPane.showMessageDialog(this, str.toString(), "Cảnh báo!!!", JOptionPane.INFORMATION_MESSAGE);
	    }

    }//GEN-LAST:event_jbtnThemActionPerformed

    private void jtDanhSachSanPhamMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtDanhSachSanPhamMouseClicked
	    // TODO add your handling code here:
	    jbtnMoi.setEnabled(true);
	    jbtnSua.setEnabled(true);
	    jbtnThem.setEnabled(false);
	    int dong = this.jtDanhSachSanPham.getSelectedRow();

	    int maSP = Integer.parseInt(this.tbmodel2.getValueAt(dong, 1).toString());
	    String tenSP = (String) this.tbmodel2.getValueAt(dong, 2).toString();
	    int slTon = Integer.parseInt(this.tbmodel2.getValueAt(dong, 3).toString());
	    int slTonToiThieu = Integer.parseInt(this.tbmodel2.getValueAt(dong, 4).toString());
	    int thoigianBaoHanh = Integer.parseInt(this.tbmodel2.getValueAt(dong, 5).toString());
	    String moTa = (String) this.tbmodel2.getValueAt(dong, 6).toString();
	    float giabanHienHanh = Float.parseFloat(this.tbmodel2.getValueAt(dong, 7).toString());
	    int maLSP = Integer.parseInt(this.tbmodel2.getValueAt(dong, 8).toString());


	    jtfMaSP.setText(String.valueOf(maSP));
	    jtfTenSP.setText(tenSP);
	    jtfSoLuongTonHienTai.setText(String.valueOf(slTon));
	    jtfSoLuongTonToiThieu.setText(String.valueOf(slTonToiThieu));
	    if (thoigianBaoHanh == 0) {
		    jcbTGBH.setSelectedIndex(0);
	    } else if (thoigianBaoHanh == 12) {
		    jcbTGBH.setSelectedIndex(1);
	    } else if (thoigianBaoHanh == 24) {
		    jcbTGBH.setSelectedIndex(2);
	    } else if (thoigianBaoHanh == 36) {
		    jcbTGBH.setSelectedIndex(3);
	    }

	    jtaMoTa.setText(moTa);
	    jtfGiaBan.setText(String.valueOf(giabanHienHanh));


	    for (int i = 0; i < cbModel.getSize(); i++) {
		    Integer str = ((LoaiSanPham) this.cbModel.getElementAt(i)).getMaLSP();

		    if (maLSP == str) {
			    this.cbModel.setSelectedItem(this.cbModel.getElementAt(i));
		    }
	    }
    }//GEN-LAST:event_jtDanhSachSanPhamMouseClicked

    private void jbtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnXoaActionPerformed
	    // TODO add your handling code here:
	    String str = "";
	    str = KiemTraMa();
	    if (str == "") {
		    jbtnSua.enable(true);
		    int maSP = Integer.parseInt(jtfMaSP.getText());

		    boolean kq = SanPhamDao.xoa(maSP);
		    if (kq == true) {
			    JOptionPane.showMessageDialog(this, "Xoá sản phẩm thành công ", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
			    LoadLaiDuLieu();
		    } else {
			    JOptionPane.showMessageDialog(this, "Xoá sản phẩm thất bại", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);
		    }
	    } else {
		    JOptionPane.showMessageDialog(this, str.toString(), "Cảnh báo!!!", JOptionPane.INFORMATION_MESSAGE);
	    }
    }//GEN-LAST:event_jbtnXoaActionPerformed

    private void jbtnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSuaActionPerformed
	    // TODO add your handling code here:
	    String str = "";
	    str = KiemTraDuLieu();
	    if (kiemTraLoiNhapSaiSo()) {
		    return;
	    }
	    if (str == "") {
		    int maSP = Integer.parseInt(this.jtfMaSP.getText());
		    String tenSP = this.jtfTenSP.getText();
		    int slTonToiThieu = Integer.parseInt(this.jtfSoLuongTonToiThieu.getText());
		    String tg = jcbTGBH.getSelectedItem().toString();
		    int thoigianBaoHanh = Integer.valueOf(tg);

		    String moTa = this.jtaMoTa.getText();
		    float giaBanHienHanh = Float.parseFloat(this.jtfGiaBan.getText());
		    LoaiSanPham lsp = (LoaiSanPham) this.jcbLoaiSP.getSelectedItem();

		    SanPham sp = new SanPham();

		    sp.setMaSP(maSP);
		    sp.setTenSP(tenSP);
		    sp.setSlTonToiThieu(slTonToiThieu);
		    sp.setThoiGianBaoHanh(thoigianBaoHanh);
		    sp.setMoTa(moTa);
		    sp.setGiaBanHienHanh(giaBanHienHanh);
		    sp.setMaLSP(lsp.getMaLSP());

		    boolean kq = SanPhamDao.capNhat(sp);
		    if (kq == true) {
			    JOptionPane.showMessageDialog(this, "Cập nhật sản phẩm thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
			    LoadLaiDuLieu();
		    } else {
			    JOptionPane.showMessageDialog(this, "Cập nhật sản phẩm thất bại", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);
		    }
	    } else {
		    JOptionPane.showMessageDialog(this, str.toString(), "Cảnh báo!!!", JOptionPane.INFORMATION_MESSAGE);
	    }
    }//GEN-LAST:event_jbtnSuaActionPerformed

       private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
		// TODO add your handling code here:
		if (THT != 0) {
			THT -= 1;
			load(SD, THT);
		}
		setTxt();
       }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
	    // TODO add your handling code here:
	    THT = 0;
	    load(SD, THT);
	    setTxt();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
	    // TODO add your handling code here:
	    if (THT < TSD / SD) {
		    THT += 1;
		    load(SD, THT);
	    }
	    setTxt();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
	    // TODO add your handling code here:
	    THT = TSD / SD;
	    load(SD, THT);

	    setTxt();
    }//GEN-LAST:event_jButton4ActionPerformed
	public void sanPhamMoi() {
		// TODO add your handling code here:
		jtfMaSP.setText("");
		jtfTenSP.setText("");
		jtfSoLuongTonHienTai.setText("0");
		jtfSoLuongTonToiThieu.setText("10");
		jtfGiaBan.setText("");
		jtaMoTa.setText("");
		jtfTenSP.requestFocus();
		jbtnThem.setEnabled(true);
	}
    private void jbtnMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnMoiActionPerformed
	    sanPhamMoi();
    }//GEN-LAST:event_jbtnMoiActionPerformed

       private void jtDanhSachSanPhamMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtDanhSachSanPhamMouseEntered
              // TODO add your handling code here:
       }//GEN-LAST:event_jtDanhSachSanPhamMouseEntered
	private void setTxt() {
		jlPhanTrang.setText(Integer.toString(THT + 1) + "/" + Integer.toString(TSD / SD + 1));
	}
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton jButton1;
       private javax.swing.JButton jButton2;
       private javax.swing.JButton jButton3;
       private javax.swing.JButton jButton4;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel12;
       private javax.swing.JLabel jLabel13;
       private javax.swing.JLabel jLabel14;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JLabel jLabel4;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JLabel jLabel6;
       private javax.swing.JLabel jLabel7;
       private javax.swing.JLabel jLabel8;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JPanel jPanel5;
       private javax.swing.JPanel jPanel6;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JScrollPane jScrollPane2;
       private javax.swing.JButton jbtnHienThi;
       private javax.swing.JButton jbtnMoi;
       private javax.swing.JButton jbtnSua;
       private javax.swing.JButton jbtnThem;
       private javax.swing.JButton jbtnXoa;
       private javax.swing.JComboBox jcbLoaiSP;
       private javax.swing.JComboBox jcbTGBH;
       private javax.swing.JLabel jlPhanTrang;
       private javax.swing.JTable jtDanhSachSanPham;
       private javax.swing.JTextArea jtaMoTa;
       private javax.swing.JTextField jtfGiaBan;
       private javax.swing.JTextField jtfMaSP;
       private javax.swing.JTextField jtfSoLuongTonHienTai;
       private javax.swing.JTextField jtfSoLuongTonToiThieu;
       private javax.swing.JTextField jtfTenSP;
       // End of variables declaration//GEN-END:variables
}
